import Vue from 'vue'
import Router from 'vue-router'
/* Layout */
import Layout from '@/layout'

Vue.use(Router)


/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {title: '首页', icon: 'el-icon-s-home'}
    }]
  },
  {
    path: '/userManage',
    component: Layout,
    meta: {title: '用户管理', icon: 'user'},
    redirect: '/userManage/allUser',
    children: [
      {
        path: 'allUser',
        name: 'allUser',
        component: () => import('@/views/userManage/allUser'),
        meta: {title: '用户管理', icon: '', menu: "users:user"},
      }, {
        path: 'statisticsUser',
        name: 'statisticsUser',
        component: () => import('@/views/userManage/statisticsUser'),
        meta: {title: '用户统计', icon: '', menu: "users:data"},
      },
      {
        path: 'UserModify',
        name: 'UserModify',
        component: () => import('@/views/userManage/modify/UserModify'),
        hidden: true,
        meta: {title: '用户管理(编辑)', icon: '', menu: "users:user:detail"},
      },
    ]
  },
  {
    path: '/orderManage',
    component: Layout,
    meta: {title: '订单管理', icon: 'dashboard'},
    redirect: '/orderManage/groupOrder',
    children: [
      {
        path: 'groupManage',
        name: 'groupManage',
        component: () => import('@/views/orderManage/groupManage'),
        meta: {title: '拼团管理', icon: '', menu: "orders:groupbuy"},
      },
      {
        path: 'groupOrder',
        name: 'groupOrder',
        component: () => import('@/views/orderManage/groupOrder'),
        meta: {title: '拼团订单管理', icon: '', menu: 'orders:user'},
      },
      {
        path: 'bonusManage',
        name: 'bonusManage',
        component: () => import('@/views/orderManage/bonusManage'),
        meta: {title: '分红管理', icon: '', menu: 'orders:bonus'},
      },
    ]
  },
  {
    path: '/commodityManage',
    component: Layout,
    meta: {title: '商品管理', icon: 'el-icon-menu'},
    redirect: '/commodityManage/index',
    children: [
      {
        path: 'index',
        name: 'commodityList',
        component: () => import('@/views/commodityManage/index'),
        meta: {title: '商品管理', icon: '', menu: "product:product"},
      },
      {
        path: 'commodityDetail/:id(\\d+)',
        component: () => import('@/views/commodityManage/commodityDetail'),
        name: 'commodityDetail',
        hidden: true,
        meta: {title: '商品详情', noCache: true, menu: "product:product:edit"}
      },
    ]
  },
  {
    path: '/setting',
    component: Layout,
    meta: {title: '系统管理', icon: 'el-icon-setting'},
    redirect: '/setting/vipLevel',
    children: [
      {
        path: 'globalRuleMana',
        name: 'globalRuleMana',
        component: () => import('@/views/setting/globalRuleMana/index'),
        meta: {title: '全局设置', icon: '', menu: "system:globalRule"},
      },
      {
        path: 'globalModify',
        name: 'globalModify',
        component: () => import('@/views/setting/globalRuleMana/modify/index'),
        hidden: true,
        meta: {
          title: '全局设置(编辑)',
          icon: 'peoples',
          activeMenu: "/setting/globalRuleMana",
          menu: "system:globalRule:edit"
        },
      },
      {
        path: 'managerMana',
        name: 'managerMana',
        component: () => import('@/views/setting/managerMana/index'),
        meta: {title: '账号管理', icon: '', menu: "system:account"},
      },
      {
        path: 'managerManaModify',
        name: 'managerManaModify',
        component: () => import('@/views/setting/managerMana/modify/index'),
        hidden: true,
        meta: {title: '账号管理(编辑)', icon: '', activeMenu: "/setting/managerMana", menu: "system:account:edit"},
      },
      {
        path: 'index',
        name: 'setting',
        component: () => import('@/views/setting/authMana/index'),
        meta: {title: '权限管理', icon: '', menu: "system:role"},
      },
      {
        path: 'authManaModify',
        name: 'authManaModify',
        component: () => import('@/views/setting/authMana/modify/index'),
        hidden: true,
        meta: {title: '权限管理(编辑)', icon: '', activeMenu: "/setting/index", menu: "system:role:edit"},
      },
    ]
  },
  // 404 page must be placed at the end !!!
  {path: '*', redirect: '/404', hidden: true}
]

function hasPermission(menus, route) {
  if (route.meta && route.meta.menu && menus) {
    /*
     * 如果这个路由有menu属性,就需要判断用户是否拥有此menu权限
     */
    return menus.indexOf(route.meta.menu) > -1;
  } else {
    return true
  }
}

function filterAsyncRoutes(asyncRoutes, menus) {
  return asyncRoutes.filter(route => {
    if (hasPermission(menus, route)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRoutes(route.children, menus)
        return (route.children && route.children.length)
      }
      return true
    }
    return false
  })
}

function createRouter() {
  console.log(constantRoutes)
  let menus = JSON.parse(localStorage.getItem("menus"));
  let accessedRouters = filterAsyncRoutes(constantRoutes, menus);
  //console.log(accessedRouters, "accessedRouters");
  return new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
      y: 0
    }),
    routes: accessedRouters,
    routesTemp: accessedRouters,
  })
}


const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
